// model/db.js
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const User = require('../models/User')

// 用户模型
const userSchema = new Schema({
  username: { type: String, required: true, unique: true, index: true },
  phone: { type: String, required: true, unique: true, index: true },
  email: { type: String, required: true, unique: true, index: true },
  password: { type: String, required: true, select: false },
  status: { type: Boolean, default: true },
  role: { type: String, default: 'user' },
  loginLogs: [{ ip: String, time: Date, status: Boolean }],
  refreshToken: {
    type: String,
    select: false // 禁止默认查询
  },
  refreshTokenExpires: {
    type: Date,
    default: () => new Date(Date.now() + 7 * 24 * 3600 * 1000) // 默认7天有效期
  },
  usedRefreshTokens: [{ // 刷新令牌黑名单
    token: String,
    expires: Date
  }]
})

const userModel = mongoose.model('User', userSchema)

// 会议模型
const meetingSchema = new Schema({
  title: { type: String, required: true },
  startTime: Date,
  endTime: Date,
  participants: [{ user: { type: Schema.Types.ObjectId, ref: 'User' } }],
  recordings: [{ fileId: String, startTime: Date, endTime: Date }]
})

const meetingModel = mongoose.model('Meeting', meetingSchema)

// 导出模型
module.exports = { userModel: User, meetingModel }